package cn.j0n4than.api.service.impl;

import cn.j0n4than.api.entity.Admin;
import cn.j0n4than.api.mapper.AdminMapper;
import cn.j0n4than.api.service.AdminService;
import cn.j0n4than.api.utils.BcryptUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author jonathan
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    /**
     * 添加管理员
     *
     * @param admin Admin 实例
     * @return 影响条数
     */
    @Override
    public int insert(Admin admin) {
        return this.baseMapper.insert(admin);
    }

    /**
     * 删除管理员
     *
     * @param id 管理员ID
     * @return 影响条数
     */
    @Override
    public int delete(int id) {
        return this.baseMapper.deleteById(id);
    }

    /**
     * 更新管理员
     *
     * @param id    管理员ID
     * @param admin Admin 实例
     * @return 影响条数
     */
    @Override
    public int update(int id, Admin admin) {
        UpdateWrapper<Admin> adminUpdateWrapper = new UpdateWrapper<>();
        adminUpdateWrapper.eq("id", id);
        return this.baseMapper.update(admin, adminUpdateWrapper);
    }

    /**
     * 获取管理员列表(分页)
     *
     * @param page  页码
     * @param limit 数据条数
     * @return result
     */
    @Override
    public HashMap<String, Object> getList(int page, int limit) {
        HashMap<String, Object> result = new HashMap<>();
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper
                .orderByDesc("created_at")
                .select("id",
                        "username",
                        "email",
                        "last_login_ip",
                        "last_login_at",
                        "created_at");
        this.baseMapper.selectList(adminQueryWrapper);
        Page<Admin> p = new Page<>(page, limit);
        Page<Admin> adminPage = this.baseMapper.selectPage(p, adminQueryWrapper);

        result.put("list", adminPage.getRecords());
        result.put("total", adminPage.getTotal());
        result.put("current_page", page);
        result.put("limit", limit);
        return result;
    }

    /**
     * 通过ID找到管理员
     *
     * @param id 管理员ID
     * @return Admin 实例
     */
    @Override
    public Admin findAdminById(int id) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("id", id);
        return this.baseMapper.selectOne(adminQueryWrapper);
    }

    /**
     * 通过用户名找到管理员
     *
     * @param username 用户名
     * @return Admin 实例
     */
    @Override
    public Admin findAdminByUsername(String username) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username", username);
        return this.baseMapper.selectOne(adminQueryWrapper);
    }

    /**
     * 登陆验证
     *
     * @param username 用户名
     * @param password 密码
     * @return Admin 实例，验证失败返回null
     */
    @Override
    public Admin login(String username, String password) {
        Admin admin = this.findAdminByUsername(username);
        if (admin == null) {
            return null;
        }
        if (!BcryptUtil.passwordVerify(password, admin.password)) {
            return null;
        }
        return admin;
    }
}
